package com.boyunv.observer.example03;
/*
 *@description
 *事件处理类
 *@author boyunv
 *@create 2023/8/12 15:52
 *@version 1.0
 */

import com.boyunv.observer.example02.LotteryResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EventManager {
   public  enum  EventType{
    MQ,
    Message
   }
   //监听器的集合
   Map<Enum<EventType>, List<EventListener>>   listeners=new HashMap<>();

   public  EventManager(Enum<EventType>... operations){
    for (Enum<EventType> operation : operations) {
       this.listeners.put(operation,new ArrayList<>());
    }
   }

    /**
     * 订阅
     * @param eventType  事件类型
     * @param listener     监听对象
     */
   public  void    subscribe(Enum<EventType> eventType,EventListener listener){
       List<EventListener> users = listeners.get(eventType);
       users.add(listener);
   }
    /**
     * 取消订阅
     * @param eventType  事件类型
     * @param listener     监听对象
     */
    public  void    unsubscribe(Enum<EventType> eventType,EventListener listener){
        List<EventListener> users = listeners.get(eventType);
        users.remove(listener);
    }

    /**
     * 通知的方法
     * @param eventType
     * @param result
     */
    public  void  notify(Enum<EventType> eventType, LotteryResult result){
        List<EventListener> users = listeners.get(eventType);
        for (EventListener listener : users) {
            listener.doEvent(result);
        }
    }
}
